<?php
class Contact_Controller extends Template_Controller {
	
	protected $mr;
	public $template = 'client/index';
	
    public function __construct()
    {
        parent::__construct();
		//Css
        $this->template->css = $this->site['theme_url'].'register/register.css';
        //View
        $this->get_view();
    }
    
	function _get_submit()
	{
		if($this->session->get('error_msg')){
			$this->template->content->error_msg = $this->session->get('error_msg');
		}
		if($this->session->get('frm')){
			$this->template->content->mr = $this->session->get('frm');
		}
	}
	
    public function index()
    {	
        $this->template->content = new View('contact/frm');
        
        $this->_get_submit();
        
        $this->mr['str_random'] = text::random($type = 'numeric', $length = 5);

		$this->session->set_flash('sess_random',$this->mr['str_random']);
		
        $this->template->content->mr = $this->mr;
    }
    
    function _get_record()
    {
    	$form = array
	    (
	        'txt_name' => '',
	        'txt_email' => '',
	        'txt_phone' => '',
	        'txt_subject' => '',
			'txt_content' => '',
			//'txt_random' => '',
			'sel_send' => '',						
	    );
	    
	    $errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_name','required');
			$post->add_rules('txt_email','required','email');
			$post->add_rules('txt_subject','required');
			$post->add_rules('txt_content','required');
			//$post->add_callbacks('txt_random',array($this,'_check_security_code'));
			$post->add_rules('sel_send','trim');
			
			if($post->validate())
 			{
 				$form = arr::overwrite($form, $post->as_array());
 				return $form;
			} else {
				$errors = arr::overwrite($errors, $post->errors('contact_validation'));
				$error_msg = '';
				foreach($errors as $id => $name) 
					if($name) $error_msg .= '<br>'.$name;
				$this->session->set_flash('error_msg',$error_msg);
				
				url::redirect('contact');
				die();
			}
        }
    }
	
	public function _check_security_code(Validation $array, $field)
	{	//echo $this->session->get('sess_random');
		if($this->session->get('sess_random') != $array[$field])
		{
			$array->add_error($field, '_check_security_code');
		}
	}
	
    private function send_email($record)
    {
    	//Use connect() method to load Swiftmailer
		$swift = email::connect();
		 
		//From, subject
		$from = $record['txt_email'];
		$subject = 'Contact '.$this->site['site_name'];
		
		//HTML message
		$path = 'application/views/email_tpl/contact.tpl';
		$fi = fopen($path,'r+');
		$html_content = file_get_contents($path); 	
		//Replate content	
		$html_content = str_replace('#contact_name#',$record['txt_name'],$html_content);		
		$html_content = str_replace('#contact_phone#',$record['txt_phone'],$html_content);		
		$html_content = str_replace('#contact_content#',$record['txt_content'],$html_content);	
		fclose($fi);

		//Build recipient lists
		$recipients = new Swift_RecipientList;
		if($record['sel_send'] == 'admin')
			$recipients->addTo($this->site['site_email']);
		else
			$recipients->addTo($this->site['site_email2']);
		 
		//Build the HTML message
		$message = new Swift_Message($subject, $html_content, "text/html");
		 
		if($swift->send($message, $recipients, $from)){
			url::redirect('contact/thanks');
			die();
		} else {
			
		}
		// Disconnect
		$swift->disconnect();
    }
    
    public function sm()
    {
    	$record = $this->_get_record();
    	
    	if($record)
    	{
    		$this->send_email($record);
    	}
    	
    	url::redirect('contact');
		die();
    }
    
    public function thanks()
    {
    	$this->template->content = new View('contact/thanks');
    }
}
?>